package com.hainatong.shiphelper.module.handlejob;

import java.util.ArrayList;
import java.util.List;

import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentListViewBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.adapter.SubFunctionAdapter;
import com.hainatong.shiphelper.mainhome.entity.FunctionEntity;
import com.hainatong.shiphelper.module.handlejob.entity.HandleTypeBean;
import com.hainatong.shiphelper.module.handlejob.fragment.HandleWorkFragment;
import com.hainatong.shiphelper.module.handlejob.viewmodel.HandleJobViewModel;

import android.os.Bundle;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.hainatong.shiphelper.module.holiday.entity.DictFunBean;
import com.hainatong.shiphelper.utils.GsonUtil;

public class WorkMgrFragment extends MvvmLazyFragment<FragmentListViewBinding, HandleJobViewModel>
{
    private SubFunctionAdapter adapter;

    private List<DictFunBean> handleTypeList;

    private List<FunctionEntity> funList = new ArrayList<>();

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_list_view;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        adapter = new SubFunctionAdapter();
        viewDataBinding.recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        viewDataBinding.recyclerview.setHasFixedSize(true);
        viewDataBinding.recyclerview.setAdapter(adapter);

        //adapter.setList( funList );
        adapter.setOnItemClickListener((adapter1, view1, position) -> {

            DictFunBean tempFun = handleTypeList.get(position);
            HandleTypeBean handleType;
            Bundle bundle = new Bundle();
            switch ( Integer.parseInt( adapter.getItem(position).getContentTag() ) )
            {
                // 值班 交接
                case Constants.FUN_ZHIBANJIAOJIE:
                    handleType = new HandleTypeBean(HandleWorkFragment.handleWorkType,tempFun.getDict_value(),tempFun.getUuid());
                    ComFragmentActivity.startViewByParam( getActivity(),Constants.VIEW_HANDLE_JOB , GsonUtil.obj2json( handleType ));
                    break;
                // 上下班交接    ?(上下船)
                case Constants.FUN_UPDOWNJIAOJIE:
                    handleType = new HandleTypeBean(HandleWorkFragment.onShipType,tempFun.getDict_value(),tempFun.getUuid());
                    ComFragmentActivity.startViewByParam( getActivity(),Constants.VIEW_HANDLE_JOB , GsonUtil.obj2json( handleType ));
                    break;
                //离职交接
                case Constants.FUN_LIZHIJIAOJIE:
                    handleType = new HandleTypeBean(HandleWorkFragment.fireWorkType,tempFun.getDict_value(),tempFun.getUuid());
                    ComFragmentActivity.startViewByParam( getActivity(),Constants.VIEW_HANDLE_JOB , GsonUtil.obj2json( handleType ));
                    break;
                // 加班交接
                case Constants.FUN_JIABANJIAOJIE:
                    handleType = new HandleTypeBean(HandleWorkFragment.jiabanWorkType,tempFun.getDict_value(),tempFun.getUuid());
                    ComFragmentActivity.startViewByParam( getActivity(),Constants.VIEW_HANDLE_JOB , GsonUtil.obj2json( handleType ));
                    break;
                //我的交接
                case Constants.FUN_MYJIAOJIE:
                    bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_HANDLE_HISTORY);
                    ComFragmentActivity.start( getActivity(),bundle );
                    break;
            }
        });
    }

    @Override
    protected void lazyLoad()
    {
        //funList.add(new FunctionEntity(getString(R.string.job_handle),""+Constants.FUN_ZHIBANJIAOJIE,R.mipmap.ic_job_handover));
        //funList.add(new FunctionEntity(getString(R.string.ship_handle),""+Constants.FUN_UPDOWNJIAOJIE,R.mipmap.ic_up_down_board));
        //funList.add(new FunctionEntity(getString(R.string.resign_handle),""+Constants.FUN_LIZHIJIAOJIE,R.mipmap.ic_resign));


        //查询 交接类型
        viewModel.queryHandOverTypeList();
        // 交接类型 结果 处理
        viewModel.getHandleTypeResult().observe(this, result -> {
            if( result.isSuccess() )
            {
                if( result.getData() != null && !result.getData().isEmpty() )
                {
                    handleTypeList = result.getData();
                    //根据  登录用户角色显示  船长 显示 不能 适岗
                    for(int i = 0;i < handleTypeList.size();i++ )
                    {
                        DictFunBean typeBean = handleTypeList.get(i);

                        if( typeBean.getDict_value().contains("加班交接") )
                        {
                            funList.add(new FunctionEntity(typeBean.getDict_value(),""+Constants.FUN_JIABANJIAOJIE,R.mipmap.ic_job_handover));
                        }
                        else if( typeBean.getDict_value().contains("上下班交接") )
                        {
                            //应该时 上下船 交接
                            funList.add(new FunctionEntity(typeBean.getDict_value(),""+Constants.FUN_UPDOWNJIAOJIE,R.mipmap.ic_up_down_board));
                        }
                        else if( typeBean.getDict_value().contains("离职交接") )
                        {
                            funList.add(new FunctionEntity(typeBean.getDict_value(),""+Constants.FUN_LIZHIJIAOJIE,R.mipmap.ic_resign));
                        }
                        else if(typeBean.getDict_value().contains("值班交接"))
                        {
                            funList.add(new FunctionEntity(typeBean.getDict_value(),""+Constants.FUN_ZHIBANJIAOJIE,R.mipmap.ic_job_handover));
                        }
                    }
                    //最后 添加一个 我的交接
                    funList.add(new FunctionEntity(getString(R.string.my_handle),""+ Constants.FUN_MYJIAOJIE,R.mipmap.ic_handover_record));
                    // 添加 一个 空数据 避免数组越界
                    handleTypeList.add( new DictFunBean() );
                    adapter.setList( funList );
                }
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        });

    }


}
